/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.core.result.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.hanfu.domain.bo.ActiveBo;
import top.continew.admin.hanfu.domain.dto.ActiveDto;
import top.continew.admin.hanfu.domain.query.ActiveQuery;
import top.continew.admin.hanfu.domain.vo.ActiveDeVo;
import top.continew.admin.hanfu.domain.vo.ActiveListVo;
import top.continew.admin.hanfu.domain.vo.ActiveVo;
import top.continew.admin.hanfu.domain.vo.wx.ActiveDeWxVo;
import top.continew.admin.hanfu.domain.vo.wx.ActiveWxVo;
import top.continew.admin.hanfu.service.impl.ActiveServiceImpl;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-03-30
 */
@RestController
@RequestMapping("/active")
@RequiredArgsConstructor
public class ActiveController {

    private final ActiveServiceImpl activeService;

    /**
     * 创建活动
     * 
     * @param activeDto 活动dto
     */
    @PostMapping("")
    public R<String> addActive(@RequestBody ActiveDto activeDto) {

        ActiveDto active = activeService.createActive(BeanUtil.copyProperties(activeDto, ActiveBo.class));
        return R.success(active.getId() + "");
    }

    @PutMapping("")
    public R<String> putActive(@RequestBody ActiveDto activeDto) {

        ActiveBo activeBo = BeanUtil.copyProperties(activeDto, ActiveBo.class);

        activeBo.setActiveInstance(activeDto.getInstanceOssIds());

        ActiveDto activeDto1 = activeService.putActive(activeBo);

        return R.success();
    }

    @PutMapping("/head_img")
    public R<String> updateActiveImg(@RequestParam("img") List<MultipartFile> file,
                                     @RequestParam("id") Integer activeId,
                                     @RequestParam("userId") Integer userId,
                                     @RequestParam("ossIds") String ossId) {

        ActiveBo activeBo = new ActiveBo();

        activeBo.setActiveHeadImg(file);

        activeBo.setId(activeId);
        activeBo.setUpdateBy(userId + "");
        activeBo.setActiveHeadImgOssIds(ossId);

        activeService.putActiveHeadImg(activeBo);

        return R.success();
    }

    @DeleteMapping("/instance")
    public R<String> delInstanceFile(@RequestParam("id") Integer activeId,
                                     @RequestParam("userId") Integer userId,
                                     @RequestParam("ossIds") String ossId) {

        ActiveBo activeBo = new ActiveBo();
        activeBo.setId(activeId);
        activeBo.setUpdateBy(userId + "");
        activeBo.setActiveInstance(ossId);

        activeService.putActiveInstance(activeBo);

        return R.success();
    }

    /**
     * 查询活动列表
     * 
     * @param activeQuery 查询条件
     */
    @PostMapping("/get")
    public R<Page<ActiveListVo>> getActiveListVo(@RequestBody ActiveQuery activeQuery) {

        ActiveBo activeBo = new ActiveBo();

        activeBo.setActiveAddr(!activeQuery.getAddr().isEmpty() ? activeQuery.getAddr() : null);
        activeBo.setActiveStartTime(!activeQuery.getStartDate().isEmpty() ? activeQuery.getStartDate() : null);
        activeBo.setActiveDeadline(!activeQuery.getDeadlineDate().isEmpty() ? activeQuery.getDeadlineDate() : null);
        activeBo.setActiveTitle(!activeQuery.getTitle().isEmpty() ? activeQuery.getTitle() : null);

        Page<ActiveListVo> activeListVo = activeService.getActiveListVo(activeBo, activeQuery.getPageNum(), activeQuery
            .getPageSize());

        activeListVo.getRecords().forEach(i -> {
            i.setUpdateTime(DateUtil.date(Long.parseLong(i.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });

        return R.success(activeListVo);
    }

    /**
     * 查询活动详情
     * 
     * @param id 活动id
     */
    @GetMapping("/{id}")
    public R<ActiveVo> getActiveDe(@PathVariable("id") Integer id) {

        ActiveVo activeDeById = activeService.getActiveDeById(id);

        return R.success(activeDeById);
    }

    @GetMapping("/de/{id}")
    public R<ActiveDeVo> getActiveDeVo(@PathVariable("id") Integer id) {

        ActiveBo activeBo = new ActiveBo();
        activeBo.setId(id);

        ActiveDeVo activeDeVo = activeService.getActiveDeVo(activeBo);
        return R.success(activeDeVo);
    }

    /**
     * 删除活动
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delActive(@PathVariable("id") Integer id) {

        ActiveBo activeBo = new ActiveBo();

        activeBo.setId(id);

        Integer i = activeService.delActive(activeBo);

        return i == 1 ? R.success() : R.fail("删除失败");
    }

    @SaIgnore
    @GetMapping("/wx/{id}")
    public R<ActiveDeWxVo> getActiveDeWxVo(@PathVariable("id") Integer id) {

        ActiveBo activeBo = new ActiveBo();

        activeBo.setId(id);

        ActiveDeWxVo activeDeWxVo = activeService.getActiveDeWxVo(activeBo);

        return activeDeWxVo == null ? R.fail("活动错误") : R.success(activeDeWxVo);
    }

    @SaIgnore
    @PostMapping("/wx/get")
    public R<Page<ActiveWxVo>> getActiveWxVoPage(@RequestBody ActiveQuery activeQuery) {

        ActiveBo activeBo = new ActiveBo();

        activeBo.setActiveTitle(activeQuery.getTitle());

        Page<ActiveWxVo> activeWxVoPage = activeService.getActiveWxVoPage(activeBo, activeQuery
            .getPageNum(), activeQuery.getPageSize());

        return R.success(activeWxVoPage);
    }

}
